import gtk
import glib
import hildondesktop
import dbus
import time
import math
import cairo
import sys
import n900dev
import ui
import log
#import sched
import thread
import gobject

class HomeScreen(hildondesktop.HomePluginItem):


    def __init__(self):

        hildondesktop.HomePluginItem.__init__(self)
        self.log = log.log()
        self.gps = n900dev.GPSCh(self.update_gps)
        self.cell = n900dev.CellCh(self.update_cell)
        self.loop = glib.MainLoop()
        self.gps.register(self.loop)
        self.cell.register(self.loop)
        self.gps.on()

        self.wifi = n900dev.Wlan()
        self.acc = n900dev.Accel()
        
        #self.schedule = sched.scheduler(time.time, time.sleep)

        #time.sleep(3)


        self.CNT = 0
        #self.timeout_handler = glib.timeout_add(1000, self.updates, self)
        glib.timeout_add(2000, self.update_acc, self)
        glib.timeout_add(30000, self.update_wifi, self)
        
        self.widgetui = ui.ui(self)        
        self.widgetui.draw()
        self.init_log()
#        time.sleep(15)
        #self.loop.run()
        

    def init_log(self):

        init_msg = {'init':time.time()}
        self.log.save(init_msg)
        
        cell = n900dev.Celluar()
        cell.read()
        msg = {'cell':cell.cell}
        self.log.save(msg)
        self.widgetui.set_cell(cell.cell['cid'])

    def update_gps(self):
        #print "gps  " + str(time.time())
        msg = {'gps':self.gps.coords}
        self.log.save(msg)
        if "lat" in self.gps.coords:
            self.widgetui.set_lat(self.gps.coords['lat'])
        if "lon" in self.gps.coords:
            self.widgetui.set_lng(self.gps.coords["lon"])
        return True

    def update_cell(self):
        #print "cell  " + str(time.time())
        msg = {'cell':self.cell.cell}
        self.log.save(msg)
        self.widgetui.set_cell(self.cell.cell['cid'])
        return True

    def update_acc(self,w):
        #print "acc  " + str(time.time())
        msg = {}
        msg['t'] = time.time()
        self.acc.read(20)
        msg['acc']= self.acc.coords
        self.log.save(msg)
        return True
        
    def update_wifi(self,w):
        #print "wifi  "+ str(time.time())
        msg = {}
        self.wifi.scan()
        msg['wifi'] = self.wifi.aps
        self.log.save(msg)
        self.widgetui.set_wifi(len(self.wifi.aps)-1)
        return True
        
        
    def do_expose_event(s, e):
        a = e.area
        cr = s.window.cairo_create()
        cr.arc(a.width/2,a.height/2,a.width,0,2*math.pi)
        cr.set_source_rgba(0.0,0.0,0.0,0.625)
        cr.fill()
        hildondesktop.HomePluginItem.do_expose_event(s,e)

    def do_realize(s):
        scn = s.get_screen()
        cm = scn.get_rgba_colormap()
        s.set_colormap(scn.get_rgba_colormap())
        s.set_app_paintable(True)
        hildondesktop.HomePluginItem.do_realize(s)
    
    def do_destroy(self):
        sys.exit(0)


# The code below is just for testing purposes.
# It allows to run the widget as a standalone process.
if __name__ == "__main__":
    pass

